package com.nikitova.craps3d.controller 
{
	import com.carlcalderon.arthropod.Debug;
	import com.nikitova.craps3d.model.*;

	import org.puremvc.as3.multicore.interfaces.*;
	import org.puremvc.as3.multicore.patterns.command.*;
	import org.puremvc.as3.multicore.utilities.startupmanager.interfaces.IStartupProxy;
	import org.puremvc.as3.multicore.utilities.startupmanager.model.StartupMonitorProxy;
	import org.puremvc.as3.multicore.utilities.startupmanager.model.StartupResourceProxy;

	public class ModelPrepCommand extends SimpleCommand
	{
		private var _monitor : StartupMonitorProxy;

		override public function execute( note : INotification ) : void    
		{
			// *** register startup mananger proxy and resource proxies that need to be loaded
			var configProxy 		: ConfigProxy 		= new ConfigProxy();
			var gameConfigProxy 	: GameConfigProxy 	= new GameConfigProxy();
			var materialsProxy	 	: MaterialsProxy 	= new MaterialsProxy();
			var modelsProxy	 		: ModelsProxy 		= new ModelsProxy();
			_monitor = new StartupMonitorProxy();
			
			facade.registerProxy(configProxy);
			facade.registerProxy(gameConfigProxy);
			facade.registerProxy(materialsProxy);
			facade.registerProxy(modelsProxy);
			facade.registerProxy(_monitor);

			var rConfigProxy 		: StartupResourceProxy = makeAndRegisterStartupResource(ConfigProxy.SRNAME, configProxy);
			var rGameConfigProxy 	: StartupResourceProxy = makeAndRegisterStartupResource(GameConfigProxy.SRNAME, gameConfigProxy);
			var rMaterialsProxy 	: StartupResourceProxy = makeAndRegisterStartupResource(MaterialsProxy.SRNAME, materialsProxy);
			var rModelsProxy 		: StartupResourceProxy = makeAndRegisterStartupResource(ModelsProxy.SRNAME, modelsProxy);
			
			rGameConfigProxy.requires = [rConfigProxy];
			rMaterialsProxy.requires  = [rConfigProxy];
			rModelsProxy.requires     = [rConfigProxy];
			
			// *** register standard proxies
			facade.registerProxy(new ScreenProxy(note.getBody()));
			facade.registerProxy(new MouseManagerProxy());
		}

		private function makeAndRegisterStartupResource( proxyName : String, appResourceProxy : IStartupProxy ) : StartupResourceProxy 
		{
			Debug.log("Registered "+proxyName, Debug.PINK);
			var r : StartupResourceProxy = new StartupResourceProxy(proxyName, appResourceProxy);
			facade.registerProxy(r);
			_monitor.addResource(r);
			return r;
		}
	}
}